<template>
  <div class="app-container">
    <el-button style="margin-bottom: 20px" type="primary" @click="handleAdd">添加岗位</el-button>

    <!-- 岗位信息表格 -->
    <el-table :data="positions" style="width: 100%" border>
      <el-table-column prop="jobId" label="岗位名称" />
      <el-table-column prop="title" label="岗位名称" />
      <el-table-column prop="description" label="岗位描述" />
      <el-table-column prop="name" label="所属部门" />
<!--      <el-table-column prop="salary" label="薪资范围" />-->
      <el-table-column label="操作" width="200">
        <template #default="scope">
          <el-button size="small" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="handleDelete(scope.row.jobId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top: 20px; text-align: center">
      <el-pagination
          :page-size="pageForm.size"
          :total="pageForm.total"
          :current-page="pageForm.page"
          @current-change="handleCurrentChange"
          layout="prev, pager, next"
      />
    </div>

    <!-- 添加/编辑岗位弹窗 -->
    <el-dialog
        v-model="dialogVisible"
        :title="isEdit ? '编辑岗位' : '添加岗位'"
        width="50%"
    >
      <el-form :model="form" label-width="80px">
        <el-form-item label="岗位名称">
          <el-input v-model="form.title" />
        </el-form-item>
        <el-form-item label="岗位描述">
          <el-input v-model="form.description" type="textarea" />
        </el-form-item>
        <el-form-item label="所属部门">
          <el-input v-model="form.name" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">提交</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import {onMounted, ref} from 'vue';
import {ElMessage} from "element-plus";
import {addJobs, deleteJobs, getJobsList, updateJobs} from "@/api/jobs.js";

// 岗位数据
const positions = ref([]);

// 弹窗状态
const dialogVisible = ref(false);
const isEdit = ref(false); // 是否是编辑模式

// 表单数据
const form = ref({
  id: null,
  name: '',
  description: '',
  salary: '',
});

const pageForm = ref({
  page: 1,
  size: 10,
  total: 0
})

// 打开添加岗位弹窗
const handleAdd = () => {
  isEdit.value = false;
  form.value = { id: null, name: '', description: '', salary: '' }; // 重置表单
  dialogVisible.value = true;
};

// 打开编辑岗位弹窗
const handleEdit = (row) => {
  isEdit.value = true;
  form.value = { ...row }; // 填充表单数据
  dialogVisible.value = true;
};

// 删除岗位
const handleDelete = async (id) => {
  await deleteJobs(id);
  ElMessage.success('删除成功');
  await getRecruitList(pageForm.value)
};

// 提交表单
const handleSubmit = () => {
  if (isEdit.value) {
    // 编辑岗位
    handleUpdateRequest();
    ElMessage.success('修改成功');
    setTimeout(() => {
      getRecruitList(pageForm.value)
    }, 500);

  } else {
    // 添加岗位
    handleAddRequest();
    ElMessage.success('添加成功');
    setTimeout(() => {
      getRecruitList(pageForm.value)
    }, 500);
  }
  dialogVisible.value = false;
};

const handleCurrentChange = (page) => {
  pageForm.value.page = page;
  // 在这里可以触发数据加载逻辑
  console.log('当前页码:', page);
  getRecruitList();
};

const getRecruitList = async () => {
  const result = await getJobsList(pageForm.value)
  positions.value = result.data.records;
  pageForm.value.total = result.data.total;
  pageForm.value.page = result.data.current;
  pageForm.value.size = result.data.size;
}

const handleAddRequest = async () => {
  await addJobs(form.value)
}

const handleUpdateRequest = async () => {
  // 编辑岗位
  await updateJobs(form.value)
}

onMounted(() => {
  getRecruitList();
})


</script>

<style scoped>
.app-container {
  padding: 20px;
}
</style>
